package com.cindata

import com.cindata.dictionary.*
import com.cindata.dimension.RiskDimension
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class WarrantController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond Warrant.list(params), model: [warrantCount: Warrant.count()]
    }

    def show(Warrant warrant)
    {
        respond warrant
    }

    def create()
    {
        params["targetUri"] = request.getHeader("referer")
        respond new Warrant(params)
    }

    @Transactional
    def save(Warrant warrant)
    {
        if (warrant == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (warrant.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond warrant.errors, view: 'create'
            return
        }

        warrant.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'warrant.label', default: 'Warrant'), warrant.id])
                redirect uri: params['targetUri']
            }
            '*' { respond warrant, [status: CREATED] }
        }
    }

    def edit(Warrant warrant)
    {
        params["targetUri"] = request.getHeader("referer")
        respond warrant
    }

    @Transactional
    def update(Warrant warrant)
    {
        if (warrant == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (warrant.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond warrant.errors, view: 'edit'
            return
        }

        warrant.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'warrant.label', default: 'Warrant'), warrant.id])
                redirect uri: params['targetUri']
            }
            '*' { respond warrant, [status: OK] }
        }
    }

    @Transactional
    def delete(Warrant warrant)
    {
        def targetUri = request.getHeader("referer")
        if (warrant == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        warrant.delete flush: true

        redirect url: targetUri
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'warrant.label', default: 'Warrant'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    def ddddd()
    {
        render CollateralStage.list()
    }
}
